package com.xiaoyu.lanling.feature.guard.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.feature.guard.controller.GuardListController
import com.xiaoyu.lanling.feature.guard.data.GuardListData
import com.xiaoyu.lanling.view.OnShowFragment
import kotlinx.android.synthetic.main.guard_list_empty_view.*
import kotlinx.android.synthetic.main.guard_list_fragment.*

class GuardListFragment : OnShowFragment() {

    companion object {
        fun newInstance(type: String): GuardListFragment {
            val arguments = Bundle()
            arguments.putString(Constant.IntentKey.KEY_GUARD_TYPE, type)

            val fragment = GuardListFragment()
            fragment.arguments = arguments
            return fragment
        }
    }

    private val requestTag = Any()
    private var mController: GuardListController? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.guard_list_fragment, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)

        initData()
    }

    private fun initData() {
        val type = arguments?.getString(Constant.IntentKey.KEY_GUARD_TYPE) ?: return
        val context = context ?: return

        val listData = GuardListData(requestTag, type)
        empty_tip.text = getString(if (listData.isMyGuard) R.string.guard_list_my_guard_empty_tip else R.string.guard_list_guard_empty_tip)
        mController = GuardListController.create(context, listData, null, recyclerview)
        mController?.setEmptyCallback { empty_layout.visibility = View.VISIBLE }
        mController?.setNotEmptyCallback { empty_layout.visibility = View.GONE }
        mController?.tryToLoadFirstPageData()
    }
}